//
//  MyPageControl.m
//  youyo
//
//  Created by 威 王 on 12-5-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyPageControl.h"

@implementation MyPageControl  //实现部分

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pageIndicatorTintColor = [UIColor whiteColor];
        self.currentPageIndicatorTintColor = [UIColor redColor];
    }
    return self;
}

- (void)setImageStateNormal:(NSString *)imageName
{
    self.imagePageStateNormal = [UIImage imageNamed:imageName];
    [self updateDots];
}

- (void)setImageStateHighlighted:(NSString *)imageName
{
    self.imagePageStateHighlighted = [UIImage imageNamed:imageName];
    [self updateDots];
}

- (void)endTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent *)event { // 点击事件
    [super endTrackingWithTouch:touch withEvent:event];
    [self updateDots];
}

- (void)updateDots { // 更新显示所有的点按钮
    if(_imagePageStateNormal || _imagePageStateHighlighted)
    {
        self.pageIndicatorTintColor = [UIColor whiteColor];
        self.currentPageIndicatorTintColor = [UIColor redColor];
        
        /*NSArray *subview = self.subviews;  // 获取所有子视图
        for(NSInteger i = 0; i < [subview count]; i++)
        {
            UIImageView *dot = [subview objectAtIndex:i];  // 以下不解释, 看了基本明白
            CGRect frame = dot.frame;
            frame.size = CGSizeMake(5.0, 5.0);
            dot.frame = frame;

            dot.image = self.currentPage == i ? _imagePageStateHighlighted : _imagePageStateNormal;
        }*/
    }
}

- (void)dealloc { // 释放内存
    _imagePageStateNormal = nil;
    _imagePageStateHighlighted = nil;
}

//OK, 在添加处加入以下来实例化该对象代码:
//MyPageControl *pageControl =[[MyPageControl alloc] initWithFrame:CGRectMake(0,0, 200, 30)];
//pageControl.backgroundColor = [UIColorclearColor];
//pageControl.numberOfPages = 5;
//pageControl.currentPage = 0;
//[pageControlsetImagePageStateNormal:[UIImageimageNamed:@"pageControlStateNormal.png"]];
//[pageControl setImagePageStateHighlighted:[UIImageimageNamed:@"pageControlStateHighlighted.png"]];
//[self.view addSubview:pageControl];
//[pageControl release];

@end
